/*
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-web-praise nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.sri.ai.lpitools.gwtgui.client.components.editor;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.sri.ai.lpitools.cobra.gwtutil.dataobjects.shared.EvidenceInfo;
import com.sri.ai.lpitools.gwtgui.client.components.manager.EvidenceListener;
import com.sri.ai.lpitools.gwtgui.client.components.manager.EvidenceManager;

import edu.ycp.cs.dh.acegwt.client.ace.AceEditor;
import edu.ycp.cs.dh.acegwt.client.ace.AceEditorCallback;
import edu.ycp.cs.dh.acegwt.client.ace.AceEditorMode;
import edu.ycp.cs.dh.acegwt.client.ace.AceEditorTheme;

public class EvidenceEditor extends AceEditor implements EvidenceListener {
	
	private final EvidenceManager evidenceManager;
	private final String fontSize = "10pt";
	
	/** boolean tells if init has been called */
	private boolean init = false;
	private boolean isReadOnly;	
	public EvidenceEditor(EvidenceManager manager, final boolean isEditable){
		super(true);
		this.evidenceManager = manager;
		evidenceManager.addEvidenceListener(this);
		isReadOnly = true;
		addAttachHandler(new Handler() {
			@Override
			public void onAttachOrDetach(AttachEvent event) {
				if (event.isAttached()) {
					init();
					activeEvidenceChanged(evidenceManager.getActiveEvidence(), false);

					addOnChangeHandler(new AceEditorCallback() {
						@Override
						public void invokeAceCallback(JavaScriptObject obj) {
							evidenceManager.setCurrentText(getUnformattedString());
							evidenceManager.setCurrentTextFormated(getText());
						}
					});
				
				}
			}
			
		});
		
		
	}
	
	private void setEvidenceText(String evidenceText){
		setText(evidenceText);
		evidenceManager.setCurrentText(getUnformattedString());
	}
	
	public void reFormatText(String currentEvidenceText){
		if(evidenceManager != null){
			if(evidenceManager.getActiveEvidence() != null){
				setEvidenceText(currentEvidenceText);
			}
		}
	}
	/**
	 * Gets the model text without formatting.
	 * @return The unformatted model text.
	 */
	public String getUnformattedString()
	{
		if (init) {
			String formatted = getText();
			return formatted.replace("\n", "").replace("\t", "");
		} else {
			return "";
		}
	}
	

	
	private void init(){
		startEditor();
		setMode(AceEditorMode.LPIMODEL);
		// Set the theme of the editor. (Selected theme must have been added as a script tag as specified above.)
		setTheme(AceEditorTheme.ECLIPSE);
		setShowPrintMargin(false);
		setHighlightSelectedWord(true);
		setFontSize(fontSize);
		if(isReadOnly){
			setReadOnly(true);
		}
		init = true;
	}

	@Override
	public void activeEvidenceChanged(EvidenceInfo evidence, boolean getObjects) {

		String evidenceText = "";
		if (evidence != null) {
			evidenceText = evidence.getEvidence();

		}
		
		setEvidenceText(evidenceText);
	}

//	/**
//	 * Format the string based on the modelObject passed in
//	 * @param modelObject
//	 * @return
//	 */
//	private String formatText(ModelObject modelObject) {
//		
//		if (!init) {
//			//can't do this yet, not initted
//			return "";
//		}
//		
//		StringBuilder sb = new StringBuilder("");
//		sb.append(modelObject.getName());
//		sb.append(",\n");
//		sb.append(modelObject.getDescription());
//		sb.append(",\n\n");
//		List<String> sorts = modelObject.getSorts();
//		List<String> randomVariables = modelObject.getRandom();
//		
//		
//		for(String sort: sorts)
//		{
//			sb.append(sort);
//			sb.append(",\n");
//		}
//		sb.append("\n");
//		for(String randomVariable : randomVariables)
//		{
//			sb.append(randomVariable);
//			sb.append(",\n");
//		}
//		sb.append("\n");
//		String parfactors = modelObject.getParfactors();
//		parfactors = parfactors.replaceAll("} ", "}\n\t");
//		parfactors = parfactors.replaceAll("}\\)", "}\n\\)");
//		parfactors = parfactors.replaceAll("\\(\\{", "\\(\n\t\\{");
//		sb.append(parfactors);
//
//		return sb.toString();
//	}

}
